package com.tedu.egvehicle.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu.egvehicle.base.response.JsonResult;
import com.tedu.egvehicle.base.response.StatusCode;
import com.tedu.egvehicle.mapper.VehicleMapper;
import com.tedu.egvehicle.pojo.dto.VehicleAddParam;
import com.tedu.egvehicle.pojo.dto.VehicleListQuery;
import com.tedu.egvehicle.pojo.entity.Vehicle;
import com.tedu.egvehicle.pojo.vo.UserVO;
import com.tedu.egvehicle.pojo.vo.VehicleListVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import org.springdoc.core.service.GenericParameterService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/vehicle")
@Tag(name = "车辆模块")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;
    @Autowired
    private GenericParameterService parameterBuilder;

    @PostMapping("/add")
    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody VehicleAddParam vehicleAddParam, HttpSession session){
        UserVO user = (UserVO) session.getAttribute("user");
        if (user == null) {
            return JsonResult.fail(StatusCode.NOT_LOGIN.getMessage());
        }
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam,vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(user.getId());
        int num = vehicleMapper.insert(vehicle);
        return JsonResult.success();
    }
    @GetMapping("/list")
    @Operation(summary = "车辆列表")
    @ApiOperationSupport(order = 20)
    @Parameters({
            @Parameter(name = "brand"),
            @Parameter(name = "license"),
            @Parameter(name = "session",hidden = true),
            @Parameter(name = "vehicleListQuery",hidden = true)
    })
    public JsonResult select(VehicleListQuery vehicleListQuery,HttpSession session){
        UserVO user = (UserVO) session.getAttribute("user");
        if (user == null){
            return JsonResult.fail(StatusCode.NOT_LOGIN.getMessage());
        }
        List<VehicleListVO> vehicleListVO = vehicleMapper.select(vehicleListQuery.getBrand(),
                vehicleListQuery.getLicense(),user.getId());
        return JsonResult.success(vehicleListVO);
    }
    @PostMapping("/del/{id}")
    @Operation(summary = "删除车辆")
    @ApiOperationSupport(order = 30)
    public JsonResult delete(@PathVariable Long id,HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        int num = vehicleMapper.delete(id,userVO.getId());
        return JsonResult.success();
    }
}
